var validate={
    fno:/^([A-Z]|[a-z]|\d)(\d{4,17}|(\d{4,17}[A-Z]\d{0,4}))|(^\d{18}$)|(^\d{17}(\d|X|x)$)$/,
    ftel:/(^\d{5,6}$)|(^\d{3}-\d{5,6}$)|(^\d{8,9}$)/,
    fmail:/^([a-zA-Z]|[0-9])(\w|\-)+@(mail.foxconn.com)|(foxconn.com)$/,
    sid:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
    mail:/^([a-zA-Z0-9]+[_|_|\-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/,
    mobile:/(^\d{11}$)|(^([6|9])\d{7}$)|(^[6]([8|6])\d{5})/,
    date:/^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|[1-2][0-9]|3[0-1])$/,
    check:0,
    err:[],
    elem:"",
    BlurInit:function(){
        var elems=validate.elem.querySelectorAll("input,textarea");
        for(var i=0;i<elems.length;i++){
            if(elems[i].getAttribute("regex")!=null){
                elems[i].addEventListener("blur",function(){
                    //regex匹配
                    if(typeof validate[this.getAttribute("regex")]!="undefined"){
                        var patt=new RegExp(validate[this.getAttribute("regex")]);
                        if(!patt.test(this.value)){
                            validate.check++;
                            this.style.border="dashed 1px red";
                            validate.ShowErr(this);
                        }else{
                            this.style.border="";
                            validate.HideErr(this);
                        }
                    }
                    if(this.getAttribute("regex").indexOf("len")==0){
                        var sub=this.getAttribute("regex").split(":");
                        if(sub.length!=3){
                            validate.check++;
                            this.style.border="dashed 1px red";
                            //alert("定義錯誤，regex=len:數字:數字");
                            return;
                        }
                        if(this.value.length<sub[1] || this.value.length>sub[2]){
                            validate.check++;
                            this.style.border="dashed 1px red";
                            validate.ShowErr(this);
                            
                        }else{
                            this.style.border="";
                            validate.HideErr(this);
                        }
                    }
                })
            } 
        }
    },
    ShowErr:function(elem){
        // console.log(elem);
        if(document.querySelector("#err_"+elem.id)){
            err=document.querySelector("#err_"+elem.id);
        }else{
            var err=document.createElement("div");
            err.setAttribute("id","err_"+elem.id);
        }
        if(elem.getAttribute("regex").indexOf("len")==0){
            var sub=elem.getAttribute("regex").split(":");
            err.innerText="* 字數應該在"+sub[1]+"~"+sub[2]+"之間";
        }else{
            err.innerText="* 格式錯誤";
        }
        // err.style.position="fixed";
        err.style.position="absolute";
        err.style.color="red";
        err.style.fontSize="10px";
        // console.log(elem.getBoundingClientRect().left);
        // console.log(elem.getBoundingClientRect().top);
        // console.log(elem.offsetHeight);
        // console.log(parseInt(document.documentElement.scrollTop));
        err.style.top=(elem.getBoundingClientRect().top+elem.getBoundingClientRect().height+parseInt(document.documentElement.scrollTop))+"px";
        err.style.left=elem.getBoundingClientRect().left+"px";
        err.style.zIndex="2000";
        validate.elem.appendChild(err);
    },
    HideErr:function(elem){
        if(document.querySelector("#err_"+elem.id)){
            validate.elem.removeChild(document.querySelector("#err_"+elem.id));
        }
    },
    Submit:function(){
        validate.check=0;
        var elems=validate.elem.querySelectorAll("input,textarea");
        // console.log(elems);
        for(var i=0;i<elems.length;i++){
            if(elems[i].getAttribute("regex")!=null){
                elems[i].focus();
                elems[i].blur();
            }
        }
        if(validate.check==0){
            return true;
        }
        return false;
    },
    Init:function(){
        window.onload=function(){
            validate.elem=document.body;
            validate.BlurInit();
        }
    },
    Reset:function(){
        //重置处理
        var elems=validate.elem.querySelectorAll("input,textarea");
        for(var i=0;i<elems.length;i++){
            if(elems[i].getAttribute("regex")!=null){
                validate.HideErr(elems[i])
            }
        }
        if(document.querySelector("#err_")){
            validate.elem.removeChild(document.querySelector("#err_"));
        }
    }
}
validate.Init();
